package com.itheima.server.controller;

import com.itheima.model.vo.CountsVo;
import com.itheima.model.vo.PageResult;
import com.itheima.model.vo.SettingsVo;
import com.itheima.server.interceptor.UserHolder;
import com.itheima.server.service.SettingsService;
import com.itheima.server.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingsController {
    @Autowired
    private SettingsService settingsService;
    @Autowired
    private UserService userService;

    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo settingsVo = settingsService.settings();
        return ResponseEntity.ok(settingsVo);
    }
    /*设置陌生人问题*/
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        String content = String.valueOf(map.get("content"));
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }
    /*通知设置*/
    @PostMapping("/notifications/setting")
    public ResponseEntity notificationsSetting(@RequestBody Map map){
        settingsService.saveSettings(map);
        return ResponseEntity.ok(null);
    }
    /*分页查询黑名单*/
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int pagesize){
        PageResult pageResult=settingsService.blacklist(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }
    /*移出黑名单*/
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId){
        settingsService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 统计互相喜欢 喜欢 粉丝
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        CountsVo vo = settingsService.counts();
        return ResponseEntity.ok(vo);
    }

    /**
     * 修改手机号码 发送短信
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        userService.sendMsg(UserHolder.getMobile());
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号码 验证验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String verificationCode = map.get("verificationCode").toString();
        boolean flag = settingsService.checkCode(verificationCode);
        HashMap<String, Boolean> hashMap = new HashMap<>();
        hashMap.put("verification",flag);
        return ResponseEntity.ok(hashMap);
    }

    /**
     * 修改手机号码 保存号码
     */
    @PostMapping("/phone")
    public ResponseEntity save(@RequestBody Map map) {
        String phone = map.get("phone").toString();
        settingsService.saveNewPhone(phone);
        return ResponseEntity.ok(null);
    }

}
